﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

public class GameFormsController
{
    private const string k_FirstPlayerError = "First player empty!";
    private const string k_SecondPlayerError = "Second player empty!";
    private FormGameSettings m_FormGameSettings = new FormGameSettings();
    private FormGamePlay m_FormGamePlay = null;
    private bool gameSettingsOk = false;
    private bool userAbort = false;
    private string m_CurrentError = string.Empty;

    public void Run()
    {
        setGameSettings();
        loadGameSettings();

        if (gameSettingsOk && !userAbort)
        {
            m_FormGamePlay.ShowDialog();
        }
    }

    private void setGameSettings()
    {
        while (!gameSettingsOk && !userAbort)
        {
            if (m_FormGameSettings.ShowDialog() == DialogResult.OK)
            {
                if (validateGameSettings())
                {
                    gameSettingsOk = true;
                }
                else
                {
                    if (MessageBox.Show(
                      m_CurrentError,
                      "Error",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Error) == DialogResult.Retry)
                    {
                        setGameSettings();
                    }
                }
            }
            else
            {
                userAbort = true;
            }
        }
    }

    private bool validateGameSettings()
    {
        bool result = true;

        if (m_FormGameSettings.FirstPlayerName.Equals(string.Empty))
        {
            m_CurrentError = k_FirstPlayerError;
            result = false;
        }
        else if (m_FormGameSettings.IsTwoUserPlayers == true && m_FormGameSettings.SecondPlayerName.Equals(string.Empty))
        {
            m_CurrentError = k_SecondPlayerError;
            result = false;
        }

        return result;
    }

    private void loadGameSettings()
    {
        m_FormGamePlay = new FormGamePlay(m_FormGameSettings.RowSize, m_FormGameSettings.ColSize, m_FormGameSettings.IsTwoUserPlayers, m_FormGameSettings.FirstPlayerName, m_FormGameSettings.SecondPlayerName);
    }
}